<?php
namespace app\model;

use support\facade\Logger;
use const support\CORRECT;
use const support\THIRD_OAUTH_TYPE_1;

class MemberThirdOauthModel extends BaseModel{

    protected $table = 'rl_member_third_oauths';

    //获取用户ID
    public static function getMemberThirdOauthByOpenid(int $thirdType,string $thirdOpenid): array
    {
        $info= MemberThirdOauthModel::where([
            'third_openid'  =>$thirdOpenid,
            'third_type'    =>$thirdType,
            'status'        =>CORRECT
        ])->select(['id','member_id'])->first();
        //检测绑定用户状态
        if (!empty($info->member_id)){
            $memberId=MemberModel::where([
                'id'=>$info->member_id,
                'status'=>CORRECT
            ])->value('id');
            if (!$memberId){
                MemberThirdOauthModel::where([
                    'id'=>$info->id
                ])->update([
                    'member_id'  =>0,
                    'update_time'=>time()
                ]);
                $info->member_id=0;
            }
        }
        return [
            $info->member_id??false,
            $info->id??false,
        ];
    }

    //创建三方授权信息
    public static function createMemberThirdOauth(int $thirdType,string $thirdOpenid,int $pid,string $thirdNickname='',string $thirdAvatar='')
    {
        //获取三方APPID
        switch ($thirdType){
            case THIRD_OAUTH_TYPE_1:
                $thirdAppid=config('wechat.mini_program.app_id')?:'';
                break;
            default:
                return false;
        }
        return MemberThirdOauthModel::insertGetId([
            'third_type'    =>$thirdType,
            'third_appid'   =>$thirdAppid,
            'third_openid'  =>$thirdOpenid,
            'third_nickname'=>$thirdNickname,
            'third_avatar'  =>$thirdAvatar,
            'third_user_md5'=>md5(sprintf('%s:%s',$thirdNickname,$thirdAvatar)),
            'pid'           =>$pid,
            'create_time'   =>time()
        ]);
    }

    //绑定三方授权和用户关系
    public static function toBindMemberThirdOauthRelation(int $memberThirdOauthId,int $memberId): int
    {
        //查询三方授权头像昵称信息
        $oauth=MemberThirdOauthModel::where([
            'id'            =>$memberThirdOauthId,
            'status'        =>CORRECT
        ])->first();
        $ret= MemberThirdOauthModel::where([
            'id'            =>$memberThirdOauthId,
            'status'        =>CORRECT
        ])->update([
            'member_id'     =>$memberId,
            'update_time'   =>time()
        ]);
        if (!$ret){
            Logger::app('绑定三方授权和用户关系1','info',[$ret,$memberThirdOauthId,$memberId]);
            return false;
        }
        $oauth=$oauth->toArray();
        $ret=MemberModel::where([
            'id'            =>$memberId,
            'status'        =>CORRECT
        ])->update([
            'nickname'          =>$oauth['third_nickname'],
            'avatar'            =>$oauth['third_avatar'],
            'update_time'       =>time(),
            'last_login_time'   =>time()
        ]);
        Logger::app('绑定三方授权和用户关系2','info',[$ret,$memberThirdOauthId,$memberId,$oauth]);
        return true;
    }

}